<?php

namespace Home\Logic;

use My\Model\MallRedPacketModel;

class MallRedPacketLogic {
	
	/**
	 * 创建一个新的红包
	 * @param int $cid 客户id
	 * @param int $uid 用户id
	 * @param float $value 面值
	 * @param float $fulfil 满多少元可用
	 * @param int $endTime 0.长期有效 1.截止时间戳
	 */
	public static function newRedPacket( $cid, $uid = 0, $value = 20.00, $fulfil = 100.00, $tag, $endTime = 0 ) {
		$data = array();
		$c = preg_replace('/(0\.)|\s/', '', microtime());
		$data = array(
				'code' => $c,
				'cid' => $cid,
				'value' => $value,
				'fulfil' => $fulfil,
				'uid' => $uid,
				'efficacy' => MallRedPacketModel::EFFICACYS_UNUSED,
				'createTime' => time(),
				'endTime' => $endTime,
				'tag' => $tag,
		);
		return $data;
	}
	
	/**
	 * 添加红包
	 * @param unknown $data
	 */
	public static function addRedPacket( $data ) {
		$fun = "add";
		if ( is_numeric(key($data)) and key($data) == 0 ) {
			$fun = "addAll";
		}
		return M("MallRedPacket")->$fun($data);
	}
	
	
	/**
	 * 获得本次消费可用来抵现的红包列表
	 * @param int $uid        	
	 * @param float $money 消费总额（不含运费）
	 */
	public static function getRedPacketsUseable($uid, $money = 0) {
		$redPacketLists = M ( "MallRedPacket" )->where ( array (
								'uid' => $uid,
								'fulfil' => array (
									'elt', $money 
								),
								'endTime' => array('exp', "=0 or endTime>unix_timestamp()"),
								'efficacy' => 1,
						  ) )->select ();
		return $redPacketLists;
	}
	
	/**
	 * 随机一张红包
	 * @param int $cid 客户id
	 * @param array $value 面值不指定为任意面值
	 * 		  array(20, 100, 300)
	 * @param string $tag 标签
	 * @return boolean
	 */
	public static function RandPacket( $cid, $values = array(), $tag = '' ) {
		$code = 0;
		$condition = array( 'a.cid' => $cid, 'a.uid' => 0, 'a.endTime' => array('exp', "!=0 and a.endTime>unix_timestamp()"), );
		if ( $values ) {
			$condition['a.value'] = array('in', $values);
		}
		if ( $tag ) {
			$condition['a.tag'] = array('eq', $tag);
		}
		$count = M("MallRedPacket a")->where($condition)->count();
		if ( $count ) {
			$rowId = rand(1, $count);
			$m = M()->table("MallRedPacket a, (select @rowNO := 0) b")
					->where( $condition )
					->field("a.*,(@rowNO := @rowNo+1) AS rowNo");
			$code = M()->table($m->select(false)." a")->where("a.rowNo=$rowId")->getField("code");
		}
		return code;
	}
	
	/**
	 * 通过OpenId领取一张红包
	 * @param string $openId
	 * @param string $code 红包码
	 */
	public function takeWayByOpenId($code = '', $cid = 0, $openId = '' ) {
		$uid = M("WechatUser")->where(array('cid' => $cid, 'openId'=>$openId))->getField('id');
		return $this->takeWay($code, $cid, $uid);
	}
	
	/**
	 * 领取一张红包
	 * @param string $code 红包code(为空时自动取一张红包)
	 * @param number $cid 客户id(*)
	 * @param number $uid 用户id(*)
	 * @return boolean
	 */
	public function takeWay( $code = '', $cid = 0, $uid = 0 ) {
		if ( empty($code) ) return false;
		
		if ( false !== M("MallRedPacket")->where(array( 'cid' => $cid, 'code' => $code, 'uid' => 0, ))
						->setField(array(
								'uid' => $uid,
								'efficacy' => MallRedPacketModel::EFFICACYS_UNUSED,
								'takeAwayTime' => time(),
		)) ) {
			return true;
		}
		
		return false;
	}
	
}